//
//  SearchViewController.m
//  A_Comic
//
//  Created by sos on 16/1/23.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "SearchViewController.h"
#import <AFNetworking.h>
#import "HomePageModel.h"
@interface SearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) UISearchBar *searchBar;
@property (nonatomic,retain) NSMutableArray *dataArr;
@property (nonatomic,copy) NSString *str;
@end

@implementation SearchViewController
- (void)dealloc
{
    [_tableView release];
    [_searchBar release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.hidden = YES;
    [self.tableView release];
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    self.searchBar.placeholder = @"搜索:漫画名|作者";
//    searchBar.showsCancelButton = YES;
    self.searchBar.delegate = self;
//    self.tableView.tableHeaderView = searchBar;
    [self.view addSubview:self.searchBar];
    
    
    [self data];
    
    
    
    
    
}
-(void)data
{
    NSString *url = @"http://mobilev3.ac.qq.com/Home/homePageDetailForIosV3/uin/0/local_version/3.5.1/channel/1001/guest_id/FBB11919-34E9-4168-9491-811B4C0BDBD6";
    
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *wuArr = responseObject[@"data"][@"comicList1"][@"data"];
        
        //今日我更新临时数据数组
        NSArray *arrTwo = responseObject[@"data"][@"comicList3"][@"data"];
    
        NSArray *arrThiree = responseObject[@"data"][@"lightComic"][@"data"];
        
        //日漫经典临时数据数组
        NSArray *arrFour = responseObject[@"data"][@"manga"][@"data"];
        
        //新作上线临时数组
        NSArray *arrFir = responseObject[@"data"][@"comicList5"][@"data"];
        
        NSArray *arrSix = responseObject[@"data"][@"top"][@"data"];
        
//        NSArray *zongArr = [NSArray arrayWithObjects:wuArr,arrTwo,arrThiree,arrFour,arrFir,arrSix, nil];
        NSMutableArray *zongArr = [NSMutableArray arrayWithArray:wuArr];
        zongArr = [NSMutableArray arrayWithArray:arrTwo];
        zongArr = [NSMutableArray arrayWithArray:arrThiree];
        zongArr = [NSMutableArray arrayWithArray:arrFour];
        zongArr = [NSMutableArray arrayWithArray:arrFir];
        zongArr = [NSMutableArray arrayWithArray:arrSix];
        
        
        self.dataArr = [HomePageModel modelHandlerWithArray:zongArr];
        

            for (HomePageModel *m in self.dataArr) {
                if ([m.title isEqualToString:self.searchBar.text]) {
                    self.str = m.title;
                }
            }

        
        [self.tableView reloadData];
        

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchBar.text.length>0) {
        self.tableView.hidden = NO;
        for (HomePageModel *m in self.dataArr) {
            if ([m.title isEqualToString:searchBar.text]) {
                self.str = m.title;
            }
        }
//
        [self.tableView reloadData];
    }else{
        self.tableView.hidden = YES;
    }
    
    
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//    [self.tableView reloadData];
//    for (HomePageModel *m in self.dataArr) {
//        if ([m.title isEqualToString:searchBar.text]) {
//            self.str = m.title;
//        }
//    }
    
}
//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.str;;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
